'''
#Coding: utf-8
#Project: online-exam
#Author: rtf
#Time: 2022-07-27 18:39:10
#FileName: exam_paper.py
#Softwar: PyCharm
'''


import json
import random
from trump.query import get_item, get_items, get_table_header, modify_item
from trump.utils import ok, fail
from trump.decorators import table_headers
from utils.params_validate_decorator import validate_params
from trump.utils import get_user


@validate_params([["name", str, True], ["score", float, True], ["course_code", str, True],
                  ["single_num", float, True], ["single_score", float, True],
                  ["multiple_num", float, True], ["multiple_score", float, True],
                  ["questions_num", float, True], ["questions_score", float, True]])
async def post(app, request):
    params = request.json
    user = get_user(request)
    role = user.get("role")
    if role == "TEACHER":
        params["teacher_no"] = (user.get("teacher_no"),)
    else:
        return fail("对不起，您无此操作权限！")
    score = params.get("score")
    single_num = params.get("single_num")
    single_score = params.get("single_score")
    multiple_num = params.get("multiple_num")
    multiple_score = params.get("multiple_score")
    questions_num = params.get("questions_num")
    questions_score = params.get("questions_score")

    if score != (single_num * single_score + multiple_num * multiple_score + questions_num * questions_score):
        return fail(return_msg="分数匹配错误，请检查！")

    question_bank = await get_items(app.ctx.pool, "question_bank")
    single = list()
    multiple = list()
    questions = list()
    for x in question_bank:
        if x.get("subject_type") == "single":
            x["score"] = single_score
            single.append(x)
        elif x.get("subject_type") == "multiple":
            multiple.append(x)
            x["score"] = multiple_score
        else:
            questions.append(x)
            x["score"] = questions_score
    single_list = random.sample(single, single_num)
    multiple_list = random.sample(multiple, multiple_num)
    questions_list = random.sample(questions, questions_num)

    single_no = 1
    for x in single_list:
        x["no"] = single_no
        single_no += 1

    for x in multiple_list:
        x["no"] = single_no
        single_no += 1

    for x in questions_list:
        x["no"] = single_no
        single_no += 1

    data = [{"title": f'一、单选题（共{single_num * single_score}分，每题{single_score}分）', "questions": single_list},
            {"title": f'二、多选题（共{multiple_num * multiple_score}分，每题{multiple_score}分）', "questions": multiple_list},
            {"title": f'三、问答题（共{questions_num * questions_score}分，每题{questions_score}分）', "questions": questions_list}]
    params["exam_questions"] = json.dumps(data)


async def put(app, request, id):
    pass


async def ls(app, request):
    params = request.args
    user = get_user(request)
    role = user.get("role")
    if role == "TEACHER":
        params["teacher_no"] = (user.get("teacher_no"), )



async def post_ls(app, request, data):
    course = await get_items(app.ctx.pool, "course")
    for i in data:
        for x in course:
            if i.get("course_code") == x.get("code"):
                i["course_name"] = x.get("name")
        i["exam_questions"] = json.loads(i.get("exam_questions"))


async def get(app, request, id):
    pass


async def post_get(app, request, data, id):
    course = await get_item(app.ctx.pool, "course", data.get("course_code"), column="code")
    data["course_name"] = course.get("name") if course else None


async def delete(app, request, id):
    pass